package com.zyp.coolshark.controller;

import com.zyp.coolshark.mapper.ProductMapper;
import com.zyp.coolshark.pojo.dto.ProductDto;
import com.zyp.coolshark.pojo.dto.ProductUpdateDto;
import com.zyp.coolshark.pojo.entity.Product;
import com.zyp.coolshark.pojo.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/product/")
public class ProductController {

    @Autowired
    ProductMapper mapper;

    @RequestMapping("insert")
    public void insert(@RequestBody ProductDto productDto) {
        // 把dto数据装进entity
        Product product = new Product();
        BeanUtils.copyProperties(productDto,product);
        product.setCreated(new Date());
        product.setViewCount(0);
        // 创建ProductMapper
        mapper.insert(product);
    }

    @RequestMapping("select")
    public List<ProductAdminVo> select() {
        return mapper.selectForAdmin();
    }

    @Value("${imagesPath}")
    private String dirPath;

    @RequestMapping("delete")
    public void delete(int id) {
        String url = mapper.selectUrlById(id);
        new File(dirPath+url).delete();
        mapper.deleteById(id);
    }

    @RequestMapping("selectById")
    public ProductUpdateVo selectById(int id) {
        return mapper.selectById(id);
    }

    @RequestMapping("update")
    public void update(@RequestBody ProductUpdateDto productUpdateDto) {
        // 得到原来图片的路径
        String url = mapper.selectUrlById(productUpdateDto.getId());
        // 判断原来的路径是否和新的路径相同
        if (!url.equals(productUpdateDto.getUrl())) {
            new File(dirPath+url).delete();// 删除文件
        }


        // 增和改时需将dto转成entity
        Product product = new Product();
        product.setViewCount(0);
        product.setCreated(new Date());
        BeanUtils.copyProperties(productUpdateDto,product);
        mapper.update(product);
    }

    @RequestMapping("top")
    public List<ProductTopVo> top() {
        return mapper.selectTop();
    }

    @RequestMapping("selectAll")
    public List<ProductIndexVo> selectAll() {
        return mapper.selectForIndex();
    }

    @RequestMapping("detail")
    public ProductDetailVo detail(int id, HttpSession session) {
        String info = (String) session.getAttribute("view"+id);
        if (info == null) {
            // 让当前商品的浏览量加一
            mapper.updateViewCountById(id);
            // 把浏览过的信息存进会话对象
            session.setAttribute("view"+id,"visited");
        }
        return mapper.selectDetailById(id);
    }

    @RequestMapping("category")
    public List<ProductIndexVo> category(int id) {
        return mapper.selectByCategoryId(id);
    }

    @RequestMapping("search")
    public List<ProductIndexVo> search(String wd) {
        return mapper.selectByWd(wd);
    }
}
